@echo off
SETLOCAL

rem choose: runtime version 3.5/4/4.5
rem choose: platform mono/silverlight2/silverlight3/silverlight4/silverlight5/unity3d
rem choose: bundle mvc2/mvc3/mvc4/webapi/just serialization
rem choose: strict/non-strict
rem choose: debug/release
rem build -> merge -> publish

IF "%TARGET_RUNTIME%"=="" (
	cls
	echo ====================================================================
	echo 1. .NET Framework 3.5
	echo 2. .NET Framework 4.0
	echo 3. .NET Framework 4.5	
	echo 4. Mono
	echo ====================================================================
	IF "%TARGET_RUNTIME%"=="" ( 
		choice /C 1234 /M "Choose runtime version:"
		IF ERRORLEVEL 1 SET TARGET_RUNTIME=v3.5
		IF ERRORLEVEL 2 SET TARGET_RUNTIME=v4.0
		IF ERRORLEVEL 3 SET TARGET_RUNTIME=v4.5
		IF ERRORLEVEL 4 SET TARGET_RUNTIME=Mono
	)
)

IF "%WEB_FRAMEWORK%"=="" (
	cls
	echo ====================================================================
	echo 1. ASP.NET MVC 2
	echo 2. ASP.NET MVC 3
	echo 3. ASP.NET MVC 4
	echo 4. ASP.NET WebAPI
	echo 5. just Json Serialization
	echo ====================================================================
	choice /C 12345 /M "Choose web framework:"
	IF ERRORLEVEL 1 SET WEB_FRAMEWORK=MVC2
	IF ERRORLEVEL 2 SET WEB_FRAMEWORK=MVC3
	IF ERRORLEVEL 3 SET WEB_FRAMEWORK=MVC4
	IF ERRORLEVEL 4 SET WEB_FRAMEWORK=WEBAPI
	IF ERRORLEVEL 5 SET WEB_FRAMEWORK=SERIALIZER
)
IF "%JSON_STRICTNESS%"=="" (
	cls
	echo ====================================================================
	echo 1. Strict to standart
	echo 2. Loose
	echo ====================================================================
	choice /C 12 /M "Specify json strictness:"
	IF ERRORLEVEL 1 SET JSON_STRICTNESS=Strict
	IF ERRORLEVEL 2 SET JSON_STRICTNESS=Non Strict
)

IF "%BUILD_CONFIG%"=="" (
	cls
	echo ====================================================================
	echo 1. Debug
	echo 2. Release
	echo ====================================================================
	choice /C 12 /M "Specify build configuuration:"
	IF ERRORLEVEL 1 SET BUILD_CONFIG=Debug
	IF ERRORLEVEL 2 SET BUILD_CONFIG=Release
)


cls
echo ====================================================================
echo .NET Runtime version "%TARGET_RUNTIME%"
echo ====================================================================
echo Web Framework: "%WEB_FRAMEWORK%"
echo Json Strictness: "%JSON_STRICTNESS%"
echo Build config: "%BUILD_CONFIG%"
echo ====================================================================

IF "%NO_PAUSE%"==""	PAUSE

rem Build output
SET BUILD_OUTPUT=%cd%\Build\%WEB_FRAMEWORK% %TARGET_RUNTIME% %BUILD_CONFIG%

rem Setup MSBUILD path
IF "%MSBUILD_PATH%"=="" SET MSBUILD_PATH="%windir%\Microsoft.NET\Framework\v4.0.30319\msbuild"
IF "%TARGET_RUNTIME%"=="Mono" ( 
	SET MSBUILD_PATH="C:\Program Files (x86)\Mono-2.11.3\bin\xbuild.bat"
	SET TARGET_RUNTIME=v4.5
)

rem Ensure MSBUILD executable
IF NOT EXIST %MSBUILD_PATH% (
	echo MSBUILD at %MSBUILD_PATH% not found, please select it manually
	For /F "Tokens=1 Delims=" %%I In ('cscript //nologo browsefolderdialog.vbs') Do SET _FolderName=%%I
	

	IF EXIST "%_FolderName%\msbuild.exe" SET MSBUILD_PATH="%_FolderName=%\msbuild.exe"
	IF EXIST "%_FolderName%\xbuild.bat" SET MSBUILD_PATH="%_FolderName=%\xbuild.bat"
)

rem Evaluate build parameters
SET BUILD_PARAMS=/p:JsonStrictness="%JSON_STRICTNESS%"
IF NOT "%WEB_FRAMEWORK%"=="SERIALIZER" SET BUILD_PARAMS=%BUILD_PARAMS% /p:MvcVersion=%WEB_FRAMEWORK%
IF "%TARGET_RUNTIME%"=="Mono" SET BUILD_PARAMS=%BUILD_PARAMS% /p:NoTypedReferences=true

rem Build target
SET BUILD_TARGET=/t:"Sencha_Direct:Rebuild"
IF "%WEB_FRAMEWORK%"=="SERIALIZER" SET BUILD_TARGET=/t:"Sencha_Serialization:Rebuild" /t:"Sencha_Serialization_Bson:Rebuild"

SET BUILD_LOGPATH=%BUILD_OUTPUT%\build.log
IF NOT EXIST "%BUILD_OUTPUT%\NUL" MKDIR "%BUILD_OUTPUT%"

echo on
%MSBUILD_PATH% "%cd%\Sencha.sln" %BUILD_TARGET% /p:Configuration=%BUILD_CONFIG% /p:TargetFrameworkVersion=%TARGET_RUNTIME% /p:OutputPath="%BUILD_OUTPUT%" /flp1:LogFile="%BUILD_LOGPATH%" %BUILD_PARAMS% 
echo off

IF "%NO_PAUSE%"=="" PAUSE

ENDLOCAL